package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: dong
 * @Date: 2022/3/11 16:50
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/SimpleWord")
public class SimpleWordController {
    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    public SimpleWordController() throws IOException {
    }

    @RequestMapping(value = "/Word")
    public String showWord(HttpServletRequest request, int file_id, String file_name) {
        //file_id和file_name是为了展示如何使用参数，我们这里只用到了file_name
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //webOpen的第一个参数支持能够输出下载文件的Url相对地址或者文件在服务器上的磁盘路径两种方式
        //查看详细，请在"https://www.pageoffice.cn/"搜索“PageOffice属性或方法中涉及到的URL路径或磁盘路径的说明”
        poCtrl.webOpen("/doc/SimpleWord/" + file_name, OpenModeType.docNormalEdit, "张三");
        //如果您想返回多个值，请参考DataRegionEditController中"/Word"请求的返回方式
        return poCtrl.getHtml();
    }

    @RequestMapping(value = "/Word1")
    public String showWord1(HttpServletRequest request, int file_id, String file_name) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //打开文档
        //打开磁盘路径的话有两种写法：1.D:\\doc\\test.docx  2.file://D:/doc/test.docx
        poCtrl.webOpen("file://" + dir + "/SimpleWord/" + file_name, OpenModeType.docNormalEdit, "张三");
        return poCtrl.getHtml();
    }

    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response, int file_id, String file_name) {
        //file_id和file_name是为了展示如何使用参数，我们这里只用到了file_name
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "SimpleWord/" + file_name);
        //设置自定义保存结果，用来返回给前端页面，setCustomSaveResult的参数也可以是json字符串类型
        fs.setCustomSaveResult("ok");
        fs.close();
    }


}

